﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;//引用系统的Speech的识别
using System.Speech.Synthesis; //引用语音合成

namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 语音
    /// 2019-2-20
    /// </summary>
    public partial class frmSpk : Form
    {
        public frmSpk()
        {
            InitializeComponent();

            string[] fg = { "东方", "西方", "南方", "北方" };
            sr = new SRecognition(fg);
            button2.Enabled = false;
        }

        #region 隐藏

        static SpeechSynthesizer sy = new SpeechSynthesizer();

        public void getInfo()
        {
            //创建中午识别器
            using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-CN")))
            {
                //----------------
                //初始化命令词
                Choices conmmonds = new Choices();
                //添加命令词
                conmmonds.Add(new string[] { "你好", "傻逼你是谁", "傻逼很高兴见到你", "傻逼再见" });
                //初始化命令词管理
                GrammarBuilder gBuilder = new GrammarBuilder();
                //将命令词添加到管理中
                gBuilder.Append(conmmonds);
                //实例化命令词管理
                Grammar grammar = new Grammar(gBuilder);
                //-----------------

                //创建并加载听写语法(添加命令词汇识别的比较精准)
                recognizer.LoadGrammar(grammar);
                //为语音识别事件添加处理程序。
                recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRRecongized);
                //将输入配置到语音识别器。
                recognizer.SetInputToDefaultAudioDevice();
                //启动异步，连续语音识别。
                recognizer.RecognizeAsync(RecognizeMode.Multiple);

                //保持控制台窗口打开。
                //while (true)
                //{
                //    Console.ReadLine();
                //}
            }
        }


        //speechrecognized事件处理
        public void recognizer_SpeechRRecongized(object sender, SpeechRecognizedEventArgs e)
        {
            // Console.WriteLine("识别结果："+e.Result.Text);
            MessageBox.Show("识别结果为:" + e.Result.Text);
           
            sy.Speak(e.Result.Text);
        }

        #endregion

        private SRecognition sr;

        private void button1_Click(object sender, EventArgs e)
        {
            sr.BeginRec(textBox1);
            button1.Enabled = false;
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sr.over();
            button1.Enabled = true;
            button2.Enabled = false;
        }


    }
}
